import 'package:flutter/material.dart';
import 'package:lib_uikit/dimen/gt_dimensions_v3.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/gt_selector_v3.dart';
import 'package:lib_uikit/widgets/time_picker/model/date_mode.dart';
import 'package:lib_uikit/widgets/time_picker/model/pduration.dart';
import 'package:lib_uikit/widgets/time_picker/model/suffix.dart';
import 'package:lib_uikit/widgets/time_picker/pickers.dart';
import 'package:lib_uikit/widgets/time_picker/route/date_picker_route.dart';
import 'package:lib_uikit/widgets/time_picker/time_utils.dart';

class GTPickerWithSelectorV3 extends StatefulWidget {
  final String? subTitle;
  final PDuration startDate;
  final PDuration endDate;
  final List<GTSelectorModalV3> list;
  final ValueChanged<int>? onSingleClick;
  final PDuration Function(int index, PDuration endDate) calculateStartTimeWhenClick;
  final String confirmText;
  final String cancelText;
  final DatesCallback onDatesConfirm;
  final int? selectIndex;
  final Widget? bottomWidget;

  const GTPickerWithSelectorV3(
      {super.key,
      this.subTitle,
      required this.startDate,
      required this.endDate,
      required this.list,
      this.onSingleClick,
      required this.calculateStartTimeWhenClick,
      required this.confirmText,
      required this.cancelText,
      required this.onDatesConfirm,
      this.selectIndex,
      this.bottomWidget});

  @override
  State<StatefulWidget> createState() => _PickerWithSelectorV3();
}

class _PickerWithSelectorV3 extends State<GTPickerWithSelectorV3> {
  PDuration _startDate = PDuration();
  PDuration _endDate = PDuration();
  int _index = -1;
  bool isRTL = GTThemeProvider.instance.isRTL();

  @override
  void initState() {
    super.initState();
    _startDate = widget.startDate;
    _endDate = widget.endDate;
    _index = widget.selectIndex ?? -1;
  }

  @override
  Widget build(BuildContext context) {
    String _startText =
        "${_startDate.year}-${TimeUtils.intToStr(_startDate.month ?? 0)}-${TimeUtils.intToStr(_startDate.day ?? 0)}";
    String _endText =
        "${_endDate.year}-${TimeUtils.intToStr(_endDate.month ?? 0)}-${TimeUtils.intToStr(_endDate.day ?? 0)}";
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        if(widget.subTitle!=null)...[
          Text(
            widget.subTitle ?? "",
            style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text3.color),
          ),
          const SizedBox(
            height: 8,
          ),
        ],
        const SizedBox(
          height: 8,
        ),
        Row(
          children: [
            Expanded(
                child: GestureDetector(
              onTap: () {
                GTPickerV3.showDatePicker(context,
                    mode: DateMode.YMD,
                    suffix: Suffix(),
                    showTopView: false,
                    selectDate: _startDate,
                    confirmText: widget.confirmText,
                    cancelText: widget.cancelText, onConfirm: (PDuration date) {
                  setState(() {
                    _startDate = date;
                    widget.onDatesConfirm(_startDate, _endDate);
                  });
                });
              },
              child: Container(
                height: 40,
                decoration: BoxDecoration(
                    border: Border.all(
                      color: GTColorsV3.divider5.color,
                      width: GTDimensionsV3.strokeWidth,
                    ),
                    borderRadius: const BorderRadius.all(Radius.circular(6))),
                child: Center(
                  child: Text(
                    _startText,
                    style: gtTextStyleW500(
                      fontSize: 14,
                      color: GTColorsV3.text1.color,
                    ),
                  ),
                ),
              ),
            )),
            const SizedBox(
              width: 16,
            ),
            Transform.rotate(
              angle: isRTL ? 180 : 0,
              child: Icon(
                GTIconFont.e92c,
                size: 16,
                color: GTColorsV3.icon1.color,
              ),
            ),
            const SizedBox(
              width: 16,
            ),
            Expanded(
              child: GestureDetector(
                  onTap: () {
                    GTPickerV3.showDatePicker(context,
                        mode: DateMode.YMD,
                        suffix: Suffix(),
                        showTopView: false,
                        selectDate: _endDate,
                        confirmText: widget.confirmText,
                        cancelText: widget.cancelText, onConfirm: (PDuration date) {
                      setState(() {
                        _endDate = date;
                        if (_index >= 0) {
                          _startDate = widget.calculateStartTimeWhenClick.call(_index, _endDate);
                        }
                        widget.onDatesConfirm(_startDate, _endDate);
                      });
                    });
                  },
                  child: Container(
                    height: 40,
                    decoration: BoxDecoration(
                        border: Border.all(
                          color: GTColorsV3.divider5.color,
                          width: GTDimensionsV3.strokeWidth,
                        ),
                        borderRadius: const BorderRadius.all(Radius.circular(6))),
                    child: Center(
                      child: Text(
                        _endText,
                        style: gtTextStyleW500(
                          fontSize: 14,
                          color: GTColorsV3.text1.color,
                        ),
                      ),
                    ),
                  )),
            )
          ],
        ),
        const SizedBox(
          height: 16,
        ),
        GTSelectorV3(
          list: widget.list,
          crossAxisCount: 2,
          showCheck: false,
          selectIndexes: [_index],
          onSingleClick: (index, isChecked) {
            if (isChecked) {
              widget.onSingleClick?.call(index);
            } else {
              widget.onSingleClick?.call(-1);
            }
            PDuration newStartDate = widget.calculateStartTimeWhenClick.call(index, _endDate);
            setState(() {
              _index = index;
              _startDate = newStartDate;
              widget.onDatesConfirm(_startDate, _endDate);
            });
          },
        ),
        if (widget.bottomWidget != null) ...[widget.bottomWidget!]
      ],
    );
  }
}
